package com.panda.web.controller.system;

import java.util.List;
import java.util.Objects;
import java.util.Set;

import com.panda.common.core.controller.BaseController;
import com.panda.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.panda.common.constant.Constants;
import com.panda.common.core.domain.AjaxResult;
import com.panda.common.core.domain.entity.SysMenu;
import com.panda.common.core.domain.entity.SysUser;
import com.panda.common.core.domain.model.LoginBody;
import com.panda.common.utils.SecurityUtils;
import com.panda.framework.web.service.SysLoginService;
import com.panda.framework.web.service.SysPermissionService;
import com.panda.system.service.ISysMenuService;

/**
 * 登录验证
 * 
 * @author panda
 */
@CrossOrigin
@RestController
public class SysLoginController extends BaseController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;




    /**
     * PC登录
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid(), loginBody.getPlatform());    // 生成令牌
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }



    /**
     * APP登录
     * @param loginBody
     * @return
     */
    @PostMapping("/login")
    public AjaxResult appLogin(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        loginBody.setPlatform("2"); //区分PC端 还是APP=2
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid(), loginBody.getPlatform());
        ajax.put(Constants.TOKEN, token);      // 生成令牌
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (Objects.equals(loginUser.getPlatform(),2)){//公众号
            AjaxResult ajax = AjaxResult.success();
            ajax.put("openId", loginUser.getUserId());
            return ajax;
        }else {//后端
            SysUser user = SecurityUtils.getLoginUser().getUser();
            // 角色集合
            Set<String> roles = permissionService.getRolePermission(user);
            // 权限集合
            Set<String> permissions = permissionService.getMenuPermission(user);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("user", user);
            ajax.put("roles", roles);
            ajax.put("permissions", permissions);
            return ajax;
        }



//        SysUser user = SecurityUtils.getLoginUser().getUser();
//        // 角色集合
//        Set<String> roles = permissionService.getRolePermission(user);
//        // 权限集合
//        Set<String> permissions = permissionService.getMenuPermission(user);
//        AjaxResult ajax = AjaxResult.success();
//        ajax.put("user", user);
//        ajax.put("roles", roles);
//        ajax.put("permissions", permissions);
//        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
